<template>
	<view class="">
		<view class="s-input-wrap" style="margin:0">
			<view class="s-input-box flex-row">
				<text class="black-text">来源箱条码</text>
				<input v-model="sourceForm.sourceBarcode" @confirm="setBarcode($event, true)" class="s-input flex1" placeholder="扫描 / 输入条码" />
				<image class="s-input-icon" src="/static/images/scan.png"></image>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料编号</text>
				<text class="black-text font-30">{{sourceForm && sourceForm.productNo?sourceForm.productNo:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料名称</text>
				<text class="black-text font-30">{{sourceForm && sourceForm.productName?sourceForm.productName:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">原箱数量</text>
				<text class="black-text font-30">{{sourceForm && sourceForm.sourceBoxNum?sourceForm.sourceBoxNum:'---'}}</text>
			</view>
		</view>
		<view class="s-input-wrap" style="margin:25rpx 0 0">
			<view class="s-input-box flex-row">
				<text class="black-text">目标箱条码</text>
				<input v-model="targetForm.targetBarcode" @confirm="setBarcode" class="s-input flex1" placeholder="扫描 / 输入条码" />
				<image class="s-input-icon" src="/static/images/scan.png"></image>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料编号</text>
				<text class="black-text font-30">{{targetForm && targetForm.productNo?targetForm.productNo:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">物料名称</text>
				<text class="black-text font-30">{{targetForm && targetForm.productName?targetForm.productName:'---'}}</text>
			</view>
			<view class="s-input-box flex-row-between">
				<text class="black-text font-30">原箱数量</text>
				<text class="black-text font-30">{{targetForm && targetForm.targetBoxNum?targetForm.targetBoxNum:'---'}}</text>
			</view>
		</view>
		<view class="content-list-total flex-row-between">
			<button @click="submit" class="botton p-botton botton-big">确定</button>
		</view>
	</view>
</template>

<script>
	import { validNum } from "@/utils/common"
	import {
		infoByBarcode,
		saveBarcode
	} from "@/api/common.js"
	import {
		pack
	} from "./api.js"
	
	var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
	
	export default {
		data(){
			return{
				sourceForm:{},
				targetForm:{}
			}
		},
		methods:{
			setNum(e){
				this.$set(this.form,'afterBoxNum',validNum(e))
			},
			// 获取条码信息
			setBarcode(event,source){
				infoByBarcode(event.detail.value).then(res=>{
					if(res.code == 200){
						if(res.data.status != "NORMAL"){
							this.$modal.msg(`当前条码被锁定/冻结`);
							this.clearBarcode(source?'sourceForm':'targetForm')
							return
						}
						if(source){
							// 来源箱条码
							this.sourceForm = {
								...this.sourceForm,
								"productName": res.data.productName,
								"productNo": res.data.productNo,
								"sourceBarcode": res.data.barcode,
								"sourceBoxNum": res.data.qty,
								"storehouseAreaNo": res.data.storehouseAreaNo,
								"storehouseLocationNo": res.data.storehouseLocationNo,
								"storehouseNo": res.data.storehouseNo,
							}
						}else{
							// 目标箱条码
							this.targetForm = {
								...this.targetForm,
								"productName": res.data.productName,
								"productNo": res.data.productNo,
								"targetBarcode": res.data.barcode,
								"targetBoxNum": res.data.qty,
								"storehouseAreaNo": res.data.storehouseAreaNo,
								"storehouseLocationNo": res.data.storehouseLocationNo,
								"storehouseNo": res.data.storehouseNo,
							}
						}
					}
				})
			},
			// 清空条码信息
			clearBarcode(form){
				this.$set(this,form,{})
			},
			// 拆箱
			submit(){
				if(this.sourceForm.sourceBarcode == this.targetForm.targetBarcode){
					this.$modal.msg(`条码一致，无法合箱`);
					return
				}
				if(this.sourceForm.productNo !== this.targetForm.productNo){
					this.$modal.msg(`物料不一致，无法合箱`);
					return
				}
				if(this.sourceForm.storehouseNo !== this.targetForm.storehouseNo){
					this.$modal.msg(`仓库不一致，无法合箱`);
					return
				}
				if(this.sourceForm.storehouseAreaNo !== this.targetForm.storehouseAreaNo){
					this.$modal.msg(`库区不一致，无法合箱`);
					return
				}
				if(this.sourceForm.storehouseLocationNo !== this.targetForm.storehouseLocationNo){
					this.$modal.msg(`库位不一致，无法合箱`);
					return
				}
				let form = {...this.sourceForm, ...this.targetForm}
				form.sumNum = this.sourceForm.sourceBoxNum + this.targetForm.targetBoxNum
				this.$modal.confirm('是否确认合箱？').then(() => {
					this.$modal.loading("合箱中...")
					pack(form).then(res=>{
						if(res.code == 200){
							this.$modal.closeLoading()
							this.$modal.msgSuccess("合箱成功");
							this.clearBarcode('sourceForm')
							this.clearBarcode('targetForm')
							// 延迟1秒跳转
							setTimeout(()=>{
								uni.navigateBack();
							}, 1000);
						}
					})
				})
			},
		}
	}
</script>

<style>
</style>